# 文件名: seleniums
# 当前用户: shiping.zheng
# 当前日期: 2024/9/13
# 当前时间: 10:17
# 项目名称: Flag
"""
模块描述:
    1.登录页面 2.通过验证码redis规则全量查询验证码
"""
import requests
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import io
import base64
from PIL import Image
import pytesseract
import cv2
import numpy as np
import redis
from rediscluster import RedisCluster
from common import request_Util
from common import yaml_util

# 设置EdgeDriver路径
edge_driver_path = 'C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe'
edge_options = Options()
service = Service(edge_driver_path)
driver = webdriver.Edge(service=service, options=edge_options)
driver.get('https://xuntian-test2.tclpv.com/getechLogin')
time.sleep(2)
# 使用 XPath 定位输入框
input_element = driver.find_element(By.XPATH,"//input[@placeholder='请输入用户名']")
# 在输入框中输入文本 "admin"
input_element.send_keys("admin")
time.sleep(1)
input_element = driver.find_element(By.XPATH,"//input[@type='password']")
input_element.send_keys("tcl123")
time.sleep(1)


# 初始化 Redis 集群连接
startup_nodes = [{"host": "10.51.2.30", "port": "6379"}]  # 使用集群中的一个节点进行初始化
print('初始化 Redis 集群连接成功')
try:
    # 创建 RedisCluster 实例
    rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
    print(('创建 RedisCluster 实例成功'))
    # 查找匹配模式的键
    pattern = "auth:user:code:*"
    matching_keys = []
    # 使用 scan_iter 遍历所有匹配的键
    for key in rc.scan_iter(match=pattern):
        print(f"找到键: {key}")
        value = rc.get(key)
        print(f"键: {key}, 值: {value}")

except Exception as e:
    print(f"连接到 Redis 集群时出错: {e}")
time.sleep(1)
input_element = driver.find_element(By.XPATH,"//input[@placeholder='请输入左图结果']")
input_element.send_keys(value)
login_button = driver.find_element(By.XPATH, "//button[span[text()='登录']]")
login_button.click()
input("Press Enter to close the browser...")
